package com.jsy.common.util.redis;

import org.redisson.Redisson;
//import org.redisson.api.BatchResult;
import org.redisson.api.RBatch;
import org.redisson.api.RKeys;
import org.redisson.api.RSet;
import org.redisson.api.RSetAsync;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class redisOperate {

    private static  String REDIS_HOST = "192.168.1.100";
    private static  int REDIS_PORT = 6379;
    private static  String REDIS_PASSWORD = "123456";
    private static final int REDIS_TIMEOUT = 1000;

    private static RedissonClient redisson;

    public static RedissonClient getRedissonByFile(String configPath) {
        if (redisson != null){
            return redisson;
        }
        //从文件中读取redis配置
        Config config = null;
        try {
            config = Config.fromYAML(new File(configPath));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        redisson = Redisson.create(config);
        return redisson;
    }

    public static RedissonClient getRedisson(String code) {
        if (redisson != null){
            return redisson;
        }
        // 从代码中读取redis配置
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + REDIS_HOST + ":" + REDIS_PORT)
                .setPassword(REDIS_PASSWORD)
                .setTimeout(REDIS_TIMEOUT)
                .setDatabase(0);
        config.setCodec(new StringCodec(code));
        redisson = Redisson.create(config);
        return redisson;
    }

    public static RedissonClient getLocalRedisson() {
        return Redisson.create();
    }

    public static void batchAdd(RedissonClient redissonClient, String key,String values,int size) {
        // Redisson Pipeline 示例（Java）
        RBatch batch = redissonClient.createBatch();
        for (int i = 0; i < size; i++) {
            // 批量添加数据
//            batch.getBucket(key+i).setAsync(i);
//            batch.getSetCache(key+i).addAsync(i);
            batch.getSet(key+i).addAsync(i);
//            RSetAsync<Integer> set = batch.getSet(key + i);
//            set.addAsync(i);
        }
        //redisson版本3.16.8
//        BatchResult<?> results = batch.execute(); // 一次网络请求执行所有命令
        //redisson版本2.9.1
        List<?> results = batch.execute();
        System.out.println(results);

    }

    /**
     * 批量优化获取set集合,使用pipeline,并行执行命令
     * @param redisClient
     * @param pattern
     * @param batchSize
     * @param <T>
     * @return
     */
    public static <T> Map<String, Set<T>> batchGetSets(RedissonClient redisClient, String pattern, int batchSize) {
        Map<String, Set<T>> result = new HashMap<>();
        List<String> keyList = new ArrayList<>();
        RBatch batch = redisClient.createBatch();
        RKeys keys = redisClient.getKeys();
        Iterator<String> keyIterator = keys.getKeysByPattern(pattern).iterator();
        while (keyIterator.hasNext()) {
            String next = keyIterator.next();
            keyList.add(next);
            batch.getSet(next).readAllAsync();
        }
        List<?> results = batch.execute();
        if (results!=null){
            for (int i = 0; i < results.size(); i++) {
                result.put(keyList.get(i), (Set<T>)results.get(i));
            }
        }
        return result;
    }

    public static void batchDelete(RedissonClient redissonClient, String key,String values,int size) {
        RBatch batch = redissonClient.createBatch();
        for (int i = 0; i < size; i++) {
            batch.getBucket(key+i).deleteAsync();
        }
    }

    public static void main(String[] args) {
//        RedissonClient localRedisson = getLocalRedisson();
        REDIS_HOST = "127.0.0.1";
        REDIS_PORT = 6379;
        REDIS_PASSWORD = null;
//        RedissonClient localRedisson = getRedisson("UTF-8");
        String configPath = "F:\\jsy\\goalong\\goalong-common\\src\\main\\java\\com\\jsy\\common\\util\\redis\\redissonConfig.yaml";
        RedissonClient localRedisson = getRedissonByFile(configPath);
//        batchAdd(localRedisson, "key_","value_",20000);
        batchAdd(localRedisson, "Portal@gates","",20000);
//        Map<String, Set<Object>> trialMap = batchGetSets(localRedisson, "Portal@*", 20000);
        localRedisson.shutdown();
    }
}
